package maze.logic;

import maze.app.Maze;
import maze.model.Cell;
import maze.model.Map;
import maze.model.type.Face;
import maze.view.SpriteManager;
import maze.view.SunSprite;
import maze.view.TargetSprite;
import maze.view.Window;

import org.lwjgl.input.Keyboard;

public abstract class Logic
{
	public static float SPEED = Maze.MAZE_SIZE / 300f;

	protected TargetSprite target;
	protected SunSprite sun;

	protected Logic()
	{
		target = SpriteManager.getInstance().targetSprite;
		sun = SpriteManager.getInstance().sunSprite;
	}

	public abstract void init();

	/** do all calculations, handle input, etc. */
	public void logic()
	{
		// check if already move to destination
		if (LogicManager.getInstance().moving)
		{
			if (sun.dx * (sun.xx - sun.pos.x) >= 0 && sun.dy * (sun.yy - sun.pos.y) >= 0)
			{
				LogicManager.getInstance().moving = false;
				sun.dx = 0;
				sun.dy = 0;
				sun.xx = sun.pos.x;
				sun.yy = sun.pos.y;

				// check if target is reached
				if (sun.pos.equals(target.pos))
				{
					target.randomPosition(sun.pos);

					System.out.println("----------------");
					System.out.println("Start : " + sun.pos);
					System.out.println("Target: " + target.pos);
				}
			}
		}

		// check functional keys
		if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
		{
			System.out.println("ESC pressed");
			Window.getInstance().finished = true;
		}
		else if (Keyboard.isKeyDown(Keyboard.KEY_M))
		{
			if (Window.getInstance().tryToFire())
			{
				System.out.println("MANUAL pressed");
				LogicManager.getInstance().switchMode(LogicManager.getInstance().manualLogic);
			}
		}
		else if (Keyboard.isKeyDown(Keyboard.KEY_A))
		{
			if (Window.getInstance().tryToFire())
			{
				System.out.println("AUTO pressed");
				LogicManager.getInstance().switchMode(LogicManager.getInstance().autoLogic);
			}
		}
		else if (Keyboard.isKeyDown(Keyboard.KEY_S))
		{
			if (Window.getInstance().tryToFire())
			{
				System.out.println("SMART pressed");
				LogicManager.getInstance().switchMode(LogicManager.getInstance().smartLogic);
			}
		}
		else if (Keyboard.isKeyDown(Keyboard.KEY_R))
		{
			if (Window.getInstance().tryToFire())
			{
				System.out.println("RESET pressed");

				LogicManager.getInstance().switchMode(LogicManager.getInstance().manualLogic);

				sun.init();
				target.init();

				Map.getInstance().generate();
				Map.getInstance().display();
			}
		}
		else if (Keyboard.isKeyDown(Keyboard.KEY_P))
		{
			if (Window.getInstance().tryToFire())
			{
				System.out.println("----------------");
				System.out.println("x=" + sun.pos.x);
				System.out.println("y=" + sun.pos.y);
				System.out.println("xx=" + sun.xx);
				System.out.println("yy=" + sun.yy);
				System.out.println("dx=" + sun.dx);
				System.out.println("dy=" + sun.dy);
			}
		}
	}

	protected void move(Face face)
	{
		Cell cell = Map.getInstance().cells[sun.pos.x][sun.pos.y];

		if (face == Face.EAST)
		{
			if (cell.canWallPass(Face.EAST))
			{
				LogicManager.getInstance().moving = true;
				sun.pos.x += 1;
				sun.dx = SPEED;
				sun.xx += sun.dx;
			}
		}
		else if (face == Face.WEST)
		{
			if (cell.canWallPass(Face.WEST))
			{
				LogicManager.getInstance().moving = true;
				sun.pos.x -= 1;
				sun.dx = -SPEED;
				sun.xx += sun.dx;
			}
		}
		else if (face == Face.SOUTH)
		{
			if (cell.canWallPass(Face.SOUTH))
			{
				LogicManager.getInstance().moving = true;
				sun.pos.y -= 1;
				sun.dy = -SPEED;
				sun.yy += sun.dy;
			}
		}
		else if (face == Face.NORTH)
		{
			if (cell.canWallPass(Face.NORTH))
			{
				LogicManager.getInstance().moving = true;
				sun.pos.y += 1;
				sun.dy = SPEED;
				sun.yy += sun.dy;
			}
		}
	}
}
